package com.edu.service.impl;

import com.edu.StatusCode;
import com.edu.dao.EduCurMapper;
import com.edu.dao.EduStuMapper;
import com.edu.pojo.EduCurriculum;
import com.edu.pojo.EduStu;
import com.edu.service.EduStuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 管理员用户服务层
 *
 * @author YJF
 */
@Service
public class EduStuServiceImpl implements EduStuService {

    @Autowired
    private EduStuMapper eduStuMapper;

    @Autowired
    private EduCurMapper eduCurMapper;

    @Override
    public List<EduStu> findAll() {
        return eduStuMapper.selectAll();
    }

    @Override
    public PageInfo<EduStu> findStu(String page, String size, EduStu eduStu) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        Example example = createExample(eduStu);
        List<EduStu> eduStus = eduStuMapper.selectByExample(example);
        return new PageInfo<>(eduStus);
    }

    @Override
    public int addStu(EduStu eduStu) {
        EduStu check = new EduStu();
        check.setStuName(eduStu.getStuName());
        check.setStuPerId(eduStu.getStuPerId());
        if (eduStuMapper.selectOne(check) != null) {
            return StatusCode.REP_ERROR;
        }
        String stuPerId = eduStu.getStuPerId();
        eduStu.setStuPwd(stuPerId.substring(stuPerId.length() - 6));
        Date date = new Date();
        eduStu.setUpdateBy(eduStu.getCreateBy());
        eduStu.setCreateTime(date);
        eduStu.setUpdateTime(date);
        eduStuMapper.insertSelective(eduStu);
        return StatusCode.OK;
    }

    @Override
    public EduStu findOneById(Long stuId) {
        EduStu eduStu = eduStuMapper.selectByPrimaryKey(stuId);
        List<EduCurriculum> priCur = eduCurMapper.findPriCurByStuId(stuId);
        List<EduCurriculum> pubCur = eduCurMapper.findPubCurByCls(stuId);
//        List<EduCurriculum> pubCurs = new ArrayList<>();
//        List<EduCurriculum> priCurs = new ArrayList<>();
//        for (EduCurriculum cur : curList) {
//            if ("1".equals(cur.getIsPub())) {
//                pubCurs.add(cur);
//            } else {
//                priCurs.add(cur);
//            }
//        }
        eduStu.setPubCurs(pubCur);
        eduStu.setPriCurs(priCur);
        return eduStu;
    }

    @Override
    public int updateStu(EduStu eduStu) {
        eduStu.setUpdateTime(new Date());
        eduStuMapper.updateByPrimaryKeySelective(eduStu);
        List<EduCurriculum> curByStuId = eduCurMapper.findCurByStuId(eduStu.getStuId());
        if (curByStuId.size() > 0) {
            if (eduStu.getPubCurs().size() + eduStu.getPriCurs().size() > 0) {
                eduCurMapper.delStuPriCur(eduStu);
            }else{
                eduCurMapper.delCurByStuId(eduStu.getStuId());
            }
        }
        return 1;
    }

    @Override
    public int delById(Long id) {
        eduStuMapper.deleteByPrimaryKey(id);
        int count = eduCurMapper.delCurByStuId(id);
        return 1;
    }

    /**
     * 学生查询条件构建
     *
     * @param eduStu 查询条件
     */
    public Example createExample(EduStu eduStu) {
        Example example = new Example(EduStu.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduStu != null) {
            if (eduStu.getStuId() != null) {
                criteria.andEqualTo("stuId", eduStu.getStuId());
            }
            if (!StringUtils.isEmpty(eduStu.getStuName())) {
                criteria.andLike("stuName", "%" + eduStu.getStuName() + "%");
            }
        }
        return example;
    }
}
